﻿using System;
using System.Linq;
using EFDataModel.Enums;

namespace EFDataModel.DataCreation
{
    public class CreateSampleData
    {
        #region Member
        public EFModelContainer EfModelContainer { get; set; }

        public string[] FirstNames = new[] {"Johannes", "Kai", "Thomas", "Jonathan", "Franziska", "Peter", "Ulf", "Christian", "Ralf", "Frank", "Lisa", "Larissa", "David", "Ron", "Ronny", "Alfred", "Adolf"};
        public string[] LastNames = new[] {"Renatus", "Müller", "Kaak", "Handke", "Hanke", "Frenz", "Von-Kuick", "Granert", "Kaiser", "Hübler", "Seltmann", "Brandt"};
        public string[] MailProvider = new[] {"gmx.de", "hotmail.com", "gmx.net", "t-online.de", "web.de", "web.net", "hotmail.de", "t-online.com", "t-online.net", "gmx.de", "aol.de", "aol.com", "t4m.de", "trans4mation.de"};
        #endregion

        #region Konstruktor
        public CreateSampleData(EFModelContainer azModelContainer)
        {
            EfModelContainer = azModelContainer;
        }
        #endregion

        #region Public Functions
        /// <summary>
        /// Erstellen der ListItems, wenn diese noch keine Werte enthalten.
        /// </summary>
        public void CreateData()
        {
            EfModelContainer.Database.CreateIfNotExists();

            //Erstellen der UserCategories
            CreateUser();
            CreateDynamicDataTypes();
        }
        #endregion

        #region Private functions
        /// <summary>
        /// Anlegen von dynamischen Datentypen, zu Testzwecken wird von jedem Datentyp ein Eintrag angelegt.
        /// </summary>
        private void CreateDynamicDataTypes()
        {
            var dynamicData = from efDynamicDataType in EfModelContainer.EFDynamicDataTypeMenge select efDynamicDataType;
            if (dynamicData.Count() == 0)
            {
                DynamicDataTypeCategory dynamicDataTypeCategory = new DynamicDataTypeCategory();
                dynamicDataTypeCategory.EFModule = EFModules.DynamicData;
                dynamicDataTypeCategory.EFCategoryType = EFCategoryTypes.DynamicDataTypeCategory;
                dynamicDataTypeCategory.Name = "Mitarbeiter";
                dynamicDataTypeCategory.IsSystem = false;
                dynamicDataTypeCategory.SortId = 1;
                EfModelContainer.EFCategoryMenge.Add(dynamicDataTypeCategory);
                EfModelContainer.SaveChanges();


                EFDynamicDataType efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "Datum";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.Datetime;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);

                efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "Boolean";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.Boolean;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);

                efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "Decimal";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.Decimal;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);

                efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "EMail";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.EMail;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);

                efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "Integer";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.Integer;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);

                efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "List";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.List;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);
                EfModelContainer.SaveChanges();

                DynamicDataTypeListEntry dynamicDataTypeListEntry = new DynamicDataTypeListEntry();
                dynamicDataTypeListEntry.Name = "Hauptmenü";
                dynamicDataTypeListEntry.EFDynamicDataTypeID = efDynamicDataType.ID;
                EfModelContainer.DynamicDataTypeListEntryMenge.Add(dynamicDataTypeListEntry);

                dynamicDataTypeListEntry = new DynamicDataTypeListEntry();
                dynamicDataTypeListEntry.Name = "Vorspeise";
                dynamicDataTypeListEntry.EFDynamicDataTypeID = efDynamicDataType.ID;
                EfModelContainer.DynamicDataTypeListEntryMenge.Add(dynamicDataTypeListEntry);

                dynamicDataTypeListEntry = new DynamicDataTypeListEntry();
                dynamicDataTypeListEntry.Name = "Nachspeise";
                dynamicDataTypeListEntry.EFDynamicDataTypeID = efDynamicDataType.ID;
                EfModelContainer.DynamicDataTypeListEntryMenge.Add(dynamicDataTypeListEntry);

                dynamicDataTypeListEntry = new DynamicDataTypeListEntry();
                dynamicDataTypeListEntry.Name = "Vorspeise I";
                dynamicDataTypeListEntry.EFDynamicDataTypeID = efDynamicDataType.ID;
                EfModelContainer.DynamicDataTypeListEntryMenge.Add(dynamicDataTypeListEntry);

                dynamicDataTypeListEntry = new DynamicDataTypeListEntry();
                dynamicDataTypeListEntry.Name = "Vorspeise II";
                dynamicDataTypeListEntry.EFDynamicDataTypeID = efDynamicDataType.ID;
                EfModelContainer.DynamicDataTypeListEntryMenge.Add(dynamicDataTypeListEntry);

                dynamicDataTypeListEntry = new DynamicDataTypeListEntry();
                dynamicDataTypeListEntry.Name = "Vorspeise III";
                dynamicDataTypeListEntry.EFDynamicDataTypeID = efDynamicDataType.ID;
                EfModelContainer.DynamicDataTypeListEntryMenge.Add(dynamicDataTypeListEntry);
                EfModelContainer.SaveChanges();

                efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "String";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.String;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);

                efDynamicDataType = new EFDynamicDataType();
                efDynamicDataType.CreatorID = 1;
                efDynamicDataType.CreationDate = DateTime.Now;
                efDynamicDataType.Name = "Textblock";
                efDynamicDataType.DynamicDataType = EFDynamicDataTypes.Textblock;
                efDynamicDataType.DynamicDataTypeCategoryID = dynamicDataTypeCategory.ID;
                efDynamicDataType.Module = EFModules.Member;
                efDynamicDataType.IsVisible = true;
                efDynamicDataType.SortId = "0";
                efDynamicDataType.RegExString = string.Empty;
                EfModelContainer.EFDynamicDataTypeMenge.Add(efDynamicDataType);

                EfModelContainer.SaveChanges();
            }
        }

        /// <summary>
        /// Erstellen von Beispielusern
        /// </summary>
        private void CreateUser()
        {
            var users = from eUser in EfModelContainer.EFBaseMenge.OfType<EFUser>() select eUser;

            if (users.Count() == 0)
            {
                EFUser user = new EFUser();
                user.CreationDate = DateTime.Now;
                user.EditDate = DateTime.Now;
                user.Firstname = "Johannes";
                user.Lastname = "Renatus";
                user.LoginEmail = "jr@gmx.de";
                user.SetPassword("affe");
                user.UserType = EUserTypes.SuperUser;
                user.Module = EFModules.Member;
                user.UserCategory = (from eUserCategory in EfModelContainer.EFCategoryMenge.OfType<UserCategory>() where eUserCategory.Name == "Mitarbeiter" select eUserCategory).FirstOrDefault();
                user.UserTitle = (from eUserTitle in EfModelContainer.EFCategoryMenge.OfType<UserTitle>() where eUserTitle.TypeId == (int) EUserTitles.Man select eUserTitle).FirstOrDefault();
                EfModelContainer.EFBaseMenge.Add(user);
                EfModelContainer.SaveChanges();
                long userId = user.ID;

                user = new EFUser();
                user.CreationDate = DateTime.Now;
                user.EditorID = userId;
                user.EditDate = DateTime.Now;
                user.Firstname = "Kai";
                user.CreatorID = userId;
                user.Lastname = "Müller";
                user.LoginEmail = "km@gmx.de";
                user.SetPassword("affe");
                user.Module = EFModules.Member;
                user.UserType = EUserTypes.Registered;
                user.UserCategory = (from eUserCategory in EfModelContainer.EFCategoryMenge.OfType<UserCategory>() where eUserCategory.Name == "Mitarbeiter" select eUserCategory).FirstOrDefault();
                user.UserTitle = (from eUserTitle in EfModelContainer.EFCategoryMenge.OfType<UserTitle>() where eUserTitle.TypeId == (int)EUserTitles.Man select eUserTitle).FirstOrDefault();
                EfModelContainer.EFBaseMenge.Add(user);
                EfModelContainer.SaveChanges();
                long userId1 = user.ID;


                    Random firstname = new Random(111643);
                    Random lastname = new Random(766522);
                    Random mailprovider = new Random(44564);

                    UserCategory usercat = (from eUserCategory in EfModelContainer.EFCategoryMenge.OfType<UserCategory>() where eUserCategory.Name == "Mitarbeiter" select eUserCategory).FirstOrDefault();
                    UserTitle userTi = (from eUserTitle in EfModelContainer.EFCategoryMenge.OfType<UserTitle>() where eUserTitle.TypeId == (int)EUserTitles.Man select eUserTitle).FirstOrDefault();

                for (int i = 0; i < 20; i++)
                {
                    user = new EFUser();
                    user.CreationDate = DateTime.Now;
                    user.EditDate = DateTime.Now;
                    user.Firstname = FirstNames[firstname.Next(0, FirstNames.Length)];
                    user.CreatorID = (i % 2) == 0 ? userId : userId1;
                    user.EditorID = userId;
                    user.Lastname = LastNames[lastname.Next(0, LastNames.Length)];
                    user.LoginEmail = string.Format("{0}.{1}@{2}", user.Firstname, user.Lastname, MailProvider[mailprovider.Next(0, MailProvider.Length)]);
                    user.SetPassword("affe");
                    user.Module = EFModules.Member;

                    int userType = i%5;
                    if (userType == 0)
                    {
                        user.UserType = EUserTypes.Member;
                    }

                    if (userType == 1)
                    {
                        user.UserType = EUserTypes.ExternalUser;
                    }

                    if (userType == 2)
                    {
                        user.UserType = EUserTypes.None;
                    }

                    if (userType == 3)
                    {
                        user.UserType = EUserTypes.Registered;
                    }

                    if (userType == 4)
                    {
                        user.UserType = EUserTypes.Admin;
                    }

                    user.UserCategory = usercat;
                    user.UserTitle = userTi;
                    EfModelContainer.EFBaseMenge.Add(user);

                    //Da das Ef 2.1 auch die Daten durhcsucht die noch nicht der DB hinzugefügt wurden, ist auch diese Abfrage möglich um Accounts mit gleicher Mailadresse zu verhindern
                    //var mail = (from efUser in EfModelContainer.EFBaseMenge.OfType<EFUser>() where efUser.LoginEmail == user.LoginEmail select efUser.ID).Count();

                    //if (mail == 0)
                    //{

                    //}
                    EfModelContainer.SaveChanges();
                }

            }
        }
        #endregion


    }
}
